package com.th.supcom.pds.common.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;

public class PdsLayoutContainer extends LayoutContainer
{

    /** 存放组件对应暂用的列数的集合 */
    private final List <Integer> m_columns;
    /** 存放组件的集合 */
    private final List <LayoutContainer> m_containers;
    /** 计算总的列数 */
    private Double m_count = 0.0;

    public PdsLayoutContainer ()
    {
        m_containers = new ArrayList <LayoutContainer> ();
        m_columns = new ArrayList <Integer> ();
        this.setLayout (new ColumnLayout ());
    }

    /**
     * 初始化面板 将所有添加的组件按照比列显示在内容面板中
     */
    public void init ()
    {
        for (int i = 0; i < m_containers.size (); i++)
        {
            final LayoutContainer layoutContainer = m_containers.get (i);
            this.add (layoutContainer, new ColumnData (m_columns.get (i).doubleValue () / m_count));
        }
    }

    /**
     * 添加組件
     * 
     * @param component組件
     * @param col 暂用的列数
     */
    public void insertWidget (final Component component, final Integer col)
    {
        final LayoutContainer layoutContainer = new LayoutContainer ();
        final FormLayout layout = new FormLayout ();
        layout.setLabelWidth (53);
        layout.setLabelAlign (LabelAlign.RIGHT);
        layoutContainer.setLayout (layout);
        layoutContainer.add (component, new FormData ("100%"));
        m_containers.add (layoutContainer);
        m_columns.add (col);
        m_count = m_count + col;
    }

    /**
     * 添加组件集合
     * 
     * @param components
     * @param col
     */
    public void insertWidgets (List <Component> components, Integer col)
    {
        LayoutContainer layoutContainer = new LayoutContainer ();
        FormLayout layout = new FormLayout ();
        layout.setLabelWidth (80);
        layout.setLabelAlign (LabelAlign.RIGHT);
        layoutContainer.setLayout (layout);
        for (Component component : components)
        {
            layoutContainer.add (component, new FormData ("100%"));
        }
        m_containers.add (layoutContainer);
        m_columns.add (col);
        m_count = m_count + col;
    }
}
